FILTER_COOKIE = 'filKey';
FILTER_COUNTER = 'filCnt';
FILTER_KEY_OPT = {
	expires: 30
}
$(function () {
		$("#photoBtn").click(function () {
				if($("#photoArea span").length == 0)
				{
					$("#photoArea").append(
						'<span style="font-weight: bold;">上传图片</span>'+
						'<p>Powered by Img.ly</p>'+
						'<input type="file" name="image" id="imageFile"/>'+
						'<input type="submit" id="imageUploadSubmit" class="btn" value="上传"/>'+
						'<a href="#" onclick="$(\'#photoArea\').slideToggle(300)" title="Close" class="close"></a>'
					);
					$("#imageUploadSubmit").click(function (e) {
						e.preventDefault();
						ajaxFileUpload();
					});
				}
				$("#photoArea").slideToggle(100);
			});
		$("#filterBtn").click(function () {
			if($("#filterArea span").length == 0)
			{
				$("#filterArea").append(
					'<span style="font-weight: bold;">时间线过滤器</span>'+
					'<p>Seperate keywords with comma. [eg: twitter,hello] Also usernames <b>without</b> @</p> '+
					'<input type="text" id="iptFilter" name="iptFilter" class="filter_input"/>'+
					'<input type="submit" style="vertical-align: top; padding: 5px; margin: 9px 3px 0pt 6px;" id="filterSubmit" class="btn" value="Update">'+
					'<input type="submit" style="padding: 5px; vertical-align: top; margin-top: 9px;" id="filterReset" class="btn" value="Reset">'+
					'<input type="submit" style="padding: 5px; vertical-align: top; margin: 9px 0pt 0pt 3px;" id="filterHide" class="btn" value="Hide @">'+
					'<a class="close" title="Close" onclick="$(\'#filterArea\').slideToggle(300)" href="#"></a>'
				);
				$("#filterSubmit").click(function (e) {
					e.preventDefault();
					if ($.trim($('#iptFilter').val()).length == 0) {
						updateSentTip("Please enter at least one keyword!", 3000, "failure");
						return false;
					} else {
						$.cookie(FILTER_COOKIE, null);
						$.cookie(FILTER_COUNTER, null);
						updateSentTip("New keyword: " + $.trim($('#iptFilter').val()) + " added!", 3000, "success");
						filterEle();
					}
				});
			$("#filterReset").click(function (e) {
					e.preventDefault();
					$.cookie(FILTER_COOKIE, null);
					$.cookie(FILTER_COUNTER, null);
					$('#iptFilter').val("");
					updateSentTip("Filtered tweets have been restored!", 5000, "success");
					$('#statuses .filter').slideDown("fast");
				});
			$("#filterHide").toggle(

				function () {
					$('#statuses .reply').slideUp("fast");
					$('#filterHide').val("Show @");
				}, function () {
					$('#statuses .reply').slideDown("fast");
					$('#filterHide').val("Hide @");
				});
			}//
				$("#filterArea").slideToggle(100);
			});
		$("#symbolBtn").click(function () {
			if($("#symbols span").length == 0)
			{
				$.ajax({
					type:'GET',
					url:'inc/symbols.inc',
					timeout: 1000,
					success:function(data){
						$("#symbols").append(data); //获得表情
						$("#symbols span").click(function () { //绑定表情点击事件
							var obj = document.getElementById('textbox'); 
							var str = $(this).html();
								if(document.selection) {  
								obj.focus();  
								var sel=document.selection.createRange();  
								document.selection.empty();  
								sel.text = str;  
							} else {  
								var prefix, main, suffix;  
								prefix = obj.value.substring(0, obj.selectionStart);  
								main = obj.value.substring(obj.selectionStart, obj.selectionEnd);  
								suffix = obj.value.substring(obj.selectionEnd);  
								obj.value = prefix + str + suffix;  
							} 
							$("#symArea").hide();
							leaveWord();
						});
					}
				});
			}
			$("#symArea").toggle(); //弹出表情窗口
		});
		$("#restoreBtn").click(function () {
				$('#textbox').val($.cookie('recover'));
				updateSentTip("Your previous tweet has been restored!", 3000, "success");
			});
		$("#autoBtn").click(function(){
				if ($("#autoBtn").hasClass("pause")) {
					clearInterval(UPDATE_INTERVAL);
					$("#autoBtn").removeClass("pause").addClass("start");
					updateSentTip("暂停了自动刷新!", 3000, "success");
				} else {
					setUpdateInterval();
					$("#autoBtn").removeClass("start").addClass("pause");
					updateSentTip("开启自动刷新!", 3000, "success");
					update();
				}
			});
		$("#refreshBtn").click(function () {
				update();
				updateSentTip("正在刷新时间线...", 3000, "ing");
			});
		$("#transBtn").click(function () {
			if($("#transArea span").length == 0)
			{
				$("#transArea").append(
					'<span style="font-weight: bold; display: block; margin-bottom: 5px;">翻译设置</span>'+
					'<p>Translate tweets into'+
						'<select name="langs" style="border: 1px solid rgb(170, 170, 170); padding: 1px 2px;">'+
						'<option value="ar">Arabic</option>'+
						'<option value="zh-CN">简体中文</option>'+
						'<option value="zh-TW">繁體中文</option>'+
						'<option value="da">Danish</option>'+
						'<option value="nl">Dutch</option>'+
						'<option value="en">English</option>'+
						'<option value="fi">Finnish</option>'+
						'<option value="fr">French</option>'+
						'<option value="de">German</option>'+
						'<option value="el">Greek</option>'+
						'<option value="hu">Hungarian</option>'+
						'<option value="is">Icelandic</option>'+
						'<option value="it">Italian</option>'+
						'<option value="ja">Japanese</option>'+
						'<option value="ko">Korean</option>'+
						'<option value="lt">Lithuanian</option>'+
						'<option value="no">Norwegian</option>'+
						'<option value="pl">Polish</option>'+
						'<option value="pt">Portuguese</option>'+
						'<option value="ru">Russian</option>'+
						'<option value="es">Spanish</option>'+
						'<option value="sv">Swedish</option>'+
						'<option value="th">Thai</option>'+
						'</select>'+
					'</p>'+
					'<p>Translate my tweets into <select name="myLangs" style="border: 1px solid rgb(170, 170, 170); margin-top: 5px; padding: 1px 2px;">'+
							'<option value="ar">Arabic</option>'+
							'<option value="zh-CN">简体中文</option>'+
							'<option value="zh-TW">繁體中文</option>'+
							'<option value="da">Danish</option>'+
							'<option value="nl">Dutch</option>'+
							'<option value="en">English</option>'+
							'<option value="fi">Finnish</option>'+
							'<option value="fr">French</option>'+
							'<option value="de">German</option>'+
							'<option value="el">Greek</option>'+
							'<option value="hu">Hungarian</option>'+
							'<option value="is">Icelandic</option>'+
							'<option value="it">Italian</option>'+
							'<option value="ja">Japanese</option>'+
							'<option value="ko">Korean</option>'+
							'<option value="lt">Lithuanian</option>'+
							'<option value="no">Norwegian</option>'+
							'<option value="pl">Polish</option>'+
							'<option value="pt">Portuguese</option>'+
							'<option value="ru">Russian</option>'+
							'<option value="es">Spanish</option>'+
							'<option value="sv">Swedish</option>'+
							'<option value="th">Thai</option>'+
						'</select>'+
						'<input type="button" value="翻译" class="btn" id="translateMy" style="vertical-align: middle; padding: 3px 8px; margin-top: -3px;">'+
					'</p>'+
					'<a class="close" title="Close" onclick="$(\'#transArea\').slideToggle(300)" href="#"></a>'
				);
					var select = $('#transArea select[name=langs]');
						select.change(function(){
								var val = $(this).val();
								$.cookie('transLang', val, option);
								$.cookie('fullLang', $(this).find('option[value=' + val + ']').text(), option);
							});
						select.find('option').each(function(){
								var lang = $.cookie('transLang')
								if(lang === null){
									lang = 'en';
								}
								if($(this).val() === lang){
									$(this).attr('selected', 'selected');
								}
							});
						var mylang = $('#transArea select[name=myLangs]');
						mylang.change(function(){
								var val = $(this).val();
								$.cookie('myLangs', val, option);
							});
						mylang.find('option').each(function(){
								var lang = $.cookie('myLangs')
								if(lang === null){
									lang = 'en';
								}
								if($(this).val() === lang){
									$(this).attr('selected', 'selected');
								}
							});
				}
				$("#transArea").slideToggle(100);
			});
			
		$("#clearBtn").click(function(e){
				e.preventDefault();
				if (confirm("这会把旧的tweet从时间线里清理掉,你真的确定这么做么?")) {
					$("#statuses .timeline").each(function(){
							$(this).find("li:gt(19)").remove();
						});
				}
			});
	});
function ajaxFileUpload() {
	updateSentTip("Uploading your image...", 10000, "ing");
	$.ajaxFileUpload({
			url: 'ajax/uploadPhoto.php',
			timeout:50000,
			secureuri: false,
			fileElementId: 'imageFile',
			dataType: 'json',
			success: function (data, status) {
				if (typeof(console) !== 'undefined' && console != null) {
					console.info(data);
				}
				if (typeof(data.result) != 'undefined' && data.result == "success") {
					$("#textbox").val($("#textbox").val() + data.url);
					updateSentTip("Your image has been uploaded!", 3000, "success");
					$("#photoArea").slideToggle(100);
				} else {
					updateSentTip("Failed to upload, please try again.", 3000, "failure");
					$("#photoArea").slideToggle(100);
				}
			},
			error: function (data, status, e) {
				updateSentTip("Failed to upload, please try again.", 3000, "failure");
				$("#photoArea").slideToggle(100);
			}
		})
	return false;
}
function enableFilter() {
	if ($.cookie(FILTER_COOKIE) != null && $.cookie(FILTER_COOKIE) != "") {
		$('#iptFilter').val(recoverKeywords());
		$.cookie(FILTER_COUNTER, null);
		filterEle();
	}
}
function filterEle() {
	if ($.trim($('#iptFilter').val()).length == 0) {
		return false;
	} else {
		var objs;
		var targets = new Array();
		var keywords = keywordRegexp();
		if (keywords === $.cookie(FILTER_COOKIE)) {
			objs = $('#statuses .timeline li:not(.filter:hidden)').find('.status_word');
		} else {
			objs = $('#statuses .timeline li').find('.status_word');
		}
		var reg = new RegExp(keywords, "i");
		for (i = 0; i < objs.length; i++) {
			if (reg.test($(objs[i]).text())) {
				targets.push(objs[i]);
			}
		}
		if ($.cookie(FILTER_COUNTER) != null && $.cookie(FILTER_COUNTER) != '') {
			$.cookie(FILTER_COUNTER, targets.length + parseInt($.cookie(FILTER_COUNTER)));
		} else {
			$.cookie(FILTER_COUNTER, targets.length);
		}
		hideMatched(targets);
		$.cookie(FILTER_COOKIE, keywords);
		setCounter();
	}
}
function hideMatched(obj) {
	$(obj).parent().parent().addClass("filter").hide();
}
function isMatch(txt, keywords) {
	var reg = RegExp(keywords, "i");
	return reg.test(txt);
}
function keywordRegexp() {
	if ($.cookie(FILTER_COOKIE) === null) {
		return setFilterCookie();
	} else {
		return $.cookie(FILTER_COOKIE);
	}
}
function recoverKeywords() {
	return $.cookie(FILTER_COOKIE).replace(/\|/g, ',');
}
function setFilterCookie() {
	var strs = $('#iptFilter').val().split(",");
	var keywords = '';
	for (i = 0; i < strs.length; i++) {
		if (strs[i] == "") continue;
		keywords += strs[i] + "|";
	}
	keywords = keywords.substr(0, keywords.length - 1);
	return keywords;
}
var option = { expire: 30 };
$(document).ready(function () {
		enableFilter();
		if($.cookie('transLang') === null){
			$.cookie('transLang', 'en', option);
		}
		if($.cookie('myLangs') === null){
			$.cookie('myLangs', 'en', option);
		}
	});